local guanshi = fk.CreateSkill{
  name = "dajubei__guanshi",
}

Fk:loadTranslationTable{
  ["dajubei__guanshi"] = "观势",
  [":dajubei__guanshi"] = "每回合限一次，你可以将场上一张牌当【无中生有】或【无懈可击】使用，然后你不能再发动此技能，直到场上两种颜色的牌数量相同。",

  ["#dajubei__guanshi"] = "观势：你可以将场上的一张牌当【无中生有】或【无懈可击】使用。",

  ["@@dajubei__guanshi-forbidden"] = "观势",

  ["$dajubei__guanshi1"] = "腹中有粮则脊自直，非节盈之。",
  ["$dajubei__guanshi2"] = "气节？可当粟米果腹乎！",
}

local TuntianUtil = require "packages/tuntian_studio/utility"

local isBalance = function(room)
  local colors = {0, 0, 0}
  table.forEach(room.alive_players, function (p)
    table.forEach(p:getCardIds("ej"), function (id)
      colors[Fk:getCardById(id).color] = colors[Fk:getCardById(id).color] + 1
    end)
  end)
  return colors[1] == colors[2]
end

guanshi:addAcquireEffect(function (self, player, is_start)
  local cards = table.map({"ex_nihilo", "nullification"}, function (n)
    return player.room:printCard(n).id
  end)
  player.room:setPlayerMark(player, guanshi.name, cards)
end)

guanshi:addEffect("viewas", {
  prompt = "#dajubei__guanshi",
  pattern = "ex_nihilo,nullification",
  expand_pile = function (self, player)
    return player:getMark(guanshi.name)
  end,
  interaction = function (self, player)
    local cards = {}
    table.forEach(Fk:currentRoom().alive_players, function (p)
      table.insertTable(cards, p:getCardIds("ej"))
    end)
    local details = table.map(cards, function (id)
      return TuntianUtil.getCardData(id, Fk:currentRoom())
    end)
    return TuntianUtil.CardNameBox { choices = cards, all_choices = details, }
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and table.contains(player:getMark(guanshi.name), to_select)
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(Fk:getCardById(cards[1]).name)
    card.skillName = guanshi.name
    card:addSubcard(self.interaction.data)
    return card
  end,
  enabled_at_play = function (self, player)
    return player:getMark("@@dajubei__guanshi-forbidden") == 0
    and player:usedSkillTimes(guanshi.name, Player.HistoryTurn) < 1
    and table.find(Fk:currentRoom().alive_players, function (p)
      return #p:getCardIds("ej") > 0
    end)
  end,
  enabled_at_response = function (self, player, response)
    return player:getMark("@@dajubei__guanshi-forbidden") == 0
    and player:usedSkillTimes(guanshi.name, Player.HistoryTurn) < 1
    and not response
    and table.find(Fk:currentRoom().alive_players, function (p)
      return #p:getCardIds("ej") > 0
    end)
  end,
  after_use = function (self, player, use)
    local room = player.room
    if not isBalance(room) then
      room:setPlayerMark(player, "@@dajubei__guanshi-forbidden", 1)
    end
  end,
})

guanshi:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(guanshi.name, true) and isBalance(player.room)
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@@dajubei__guanshi-forbidden", 0)
  end
})

return guanshi